﻿using RdpClient.BLL;

namespace RdpClient.Forms
{
    public partial class FrmServerChangePassword : Form
    {
        int ErrorCount = 0;

        public FrmServerChangePassword()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
            this.MaximizeBox = false; //取消最大化按键
            this.MinimizeBox = false; //取消最小化按键
            this.ShowInTaskbar = false;//不在任务栏显示多个窗体框
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                MessageBox.Show("登录密码不可为空");
                return;
            }

            if (txtPassword.Text.Trim() != txtPassword2.Text.Trim())
            {
                MessageBox.Show("新密码与确认密码不同");
                return;
            }

            if (ErrorCount >= 3)
            {
                this.DialogResult = DialogResult.Cancel;
            }

            int _isLogin = ServerBLL.IsLogin();
            if (_isLogin == -1)
            {
                MessageBox.Show("服务器连接失败");
                return;
            }
            else if (_isLogin == 0)
            {
                MessageBox.Show("登录超时");
                return;
            }

            if (ServerBLL.UserChangePassword(txtOldPassword.Text.Trim(), txtPassword.Text.Trim()))
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("修改密码失败");
                ErrorCount++;
                return;
            }


        }

        private void txtPassword2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                // 用户按下了回车键 
                e.Handled = true; // 阻止默认的换行行为 
                btnOK_Click(btnOK, null);
            }
        }
    }
}
